﻿using System.Collections.Generic;
using System.Linq;
using DTO;

namespace DAO
{
    public class QuangCaoDAO
    {
        public static List<QuangCaoDTO> getAll()
        {
            List<QuangCaoDTO> result = new List<QuangCaoDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.QUANGCAOs.Where(dm => dm.Deleted.Equals(0)).ToList();

            foreach (var ds in query)
            {
                QuangCaoDTO dto = new QuangCaoDTO();

                dto.MaQuangCao = ds.MaQuangCao;
                dto.MaLoaiQuangCao = ds.MaLoaiQuangCao.Value;
                dto.LinkQuangCao = ds.Link;
                dto.Resource = ds.Resource;
                dto.ThoiGianDang = ds.ThoiGianDang.Value;
                dto.ThoiGianHetHan = ds.ThoiGianHetHan.Value;
                dto.MaNguoiDung = ds.MaNguoiDung.Value;

                result.Add(dto);
            }

            return result;
        }

        public static QuangCaoDTO getAlongCode(int maQuangCao)
        {
            QuangCaoDTO result = new QuangCaoDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.QUANGCAOs.Where(dm => dm.Deleted.Equals(0) &&
                dm.MaQuangCao == maQuangCao).Single();

            result.MaQuangCao = query.MaQuangCao;
            result.MaLoaiQuangCao = query.MaLoaiQuangCao.Value;
            result.LinkQuangCao = query.Link;
            result.Resource = query.Resource;
            result.ThoiGianDang = query.ThoiGianDang.Value;
            result.ThoiGianHetHan = query.ThoiGianHetHan.Value;
            result.MaNguoiDung = query.MaNguoiDung.Value;

            return result;
        }

        public static void Insert(QuangCaoDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            QUANGCAO quangcao = new QUANGCAO();
            quangcao.MaLoaiQuangCao = _insert.MaLoaiQuangCao;
            quangcao.Link = _insert.LinkQuangCao;
            quangcao.Resource = _insert.Resource;
            quangcao.ThoiGianDang = _insert.ThoiGianDang;
            quangcao.ThoiGianHetHan = _insert.ThoiGianHetHan;
            quangcao.MaNguoiDung = _insert.MaNguoiDung;
            quangcao.Deleted = _insert.Deleted;

            dbRaoVat.QUANGCAOs.InsertOnSubmit(quangcao);
            dbRaoVat.SubmitChanges();
        }

        public static void Delete(int maQuangCao)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            QUANGCAO _delete = (from n in dbRaoVat.QUANGCAOs
                                 where n.MaQuangCao == maQuangCao && n.Deleted == 0
                                 select n).Single();

            _delete.Deleted = 1;
            dbRaoVat.SubmitChanges();
        }

        public static void Update(QuangCaoDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            QUANGCAO _update = (from n in dbRaoVat.QUANGCAOs
                                where n.MaQuangCao == dto.MaQuangCao && n.Deleted == 0
                                select n).Single();

            _update.MaLoaiQuangCao = dto.MaLoaiQuangCao;
            _update.Link = dto.LinkQuangCao;
            _update.Resource = dto.Resource;
            _update.ThoiGianDang = dto.ThoiGianDang;
            _update.ThoiGianHetHan = dto.ThoiGianHetHan;
            _update.MaNguoiDung = dto.MaNguoiDung;            

            dbRaoVat.SubmitChanges();
        }
    }
}
